package com.shell.governor.modules.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.governor.authorization.po.PubRole;
import com.shell.governor.modules.po.PubBizEnt;
import com.shell.governor.modules.po.PubBizRole;
import com.shell.governor.modules.po.PubBusiness;
import com.shell.governor.modules.service.PubBizEntService;
import com.shell.governor.modules.service.PubBizResService;
import com.shell.governor.modules.service.PubBizRoleService;
import com.shell.governor.modules.service.PubBusinessService;

import tk.mybatis.mapper.entity.Example;

@RestController
public class PubBusinessController {
	@Autowired
	PubBusinessService pubBusinessService;
	@Autowired
	PubBizRoleService pubBizRoleService;
	@Autowired
	PubBizResService pubBizResService;
	@Autowired
	public PubBizEntService pubBizEntService;

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public List<PubBusiness> finBizs(String queryValue) {
		Example exmaple = new Example(PubBusiness.class);
		Example.Criteria criteria = exmaple.createCriteria();
		if (StringUtil.notEmpty(queryValue)) {
			criteria.andLike("bizName", StringUtil.getParameterLike(queryValue));
		}
		return this.pubBusinessService.selectByExample(exmaple);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg saveBiz(PubBusiness biz) {
		if (StringUtil.empty(biz.getBizId())) {
			biz.setBizId(SystemUtil.getUUID());
			this.pubBusinessService.save(biz);
		} else {
			this.pubBusinessService.updateAll(biz);
		}
		return new RetMsg("success", "��������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg delBiz(String bizId) {
		PubBizEnt bizEnt = new PubBizEnt();
		bizEnt.setBizId(bizId);
		List<PubBizEnt> bizEnts = this.pubBizEntService.selectByEntity(bizEnt);
		if ((bizEnts != null) && (bizEnts.size() > 0)) {
			return new RetMsg("warning", "��������������������������������");
		}
		this.pubBusinessService.delBiz(bizId);
		return new RetMsg("success", "��������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������������")
	public boolean checkBizCode(String bizCode, String bizId) {
		PubBusiness biz = new PubBusiness();
		biz.setBizCode(bizCode);
		biz = (PubBusiness) this.pubBusinessService.selectOne(biz);
		if (biz == null) {
			return true;
		}
		if ((StringUtil.notEmpty(bizId)) && (bizId.equals(biz.getBizId()))) {
			return true;
		}
		return false;
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������������")
	public boolean checkBizName(String bizName, String bizId) {
		PubBusiness biz = new PubBusiness();
		biz.setBizName(bizName);
		biz = (PubBusiness) this.pubBusinessService.selectOne(biz);
		if (biz == null) {
			return true;
		}
		if ((StringUtil.notEmpty(bizId)) && (bizId.equals(biz.getBizId()))) {
			return true;
		}
		return false;
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public List<PubRole> findBizRoles(String bizId) {
		return this.pubBizRoleService.findBizRoles(bizId);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������")
	public RetMsg delBizRole(String bizId, String roleId) {
		this.pubBizRoleService.delBizRole(bizId, roleId);
		return new RetMsg("success", "������������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg saveBizRole(String bizId, String roleId) {
		PubBizRole bizRole = new PubBizRole();
		bizRole.setBizId(bizId);
		bizRole.setRoleId(roleId);
		this.pubBizRoleService.save(bizRole);
		return new RetMsg("success", "������������������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg saveBizs(String bizId, String roleId, String resIds) {
		this.pubBizResService.saveBizs(bizId, roleId, resIds);
		return new RetMsg("success", "������������������");
	}
}
